<?php
	session_start();
	$_SESSION["previouspage"] = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];//remember the url of this page, used for redirect.
	if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
		header('Location: ./');
	}
	
	$personId = (int)$_GET['id'];
	$_SESSION["reportuserid"] = $personId;
	$_SESSION["deleteuserid"] = $personId;
	
	require_once 'include/config.lib.php';
	require_once 'include/database.lib.php';
	require_once 'include/friendAPI.php';
	require_once 'controller/controlInfoAccess.php';
	require_once 'include/tags.php';
	
	ConnectToDB();// Connect to the DB.
	
	// Load summary data about the person.
	$personReq = DBExecute('SELECT * FROM person AS p, (
														SELECT COUNT(DISTINCT authoringbib.bibtexid) AS authorcount FROM authoringbib WHERE authoringbib.personid=?
													) AS a, (
														SELECT COUNT(DISTINCT reading.resourceid) AS readingcount FROM reading WHERE reading.personid=?
													) AS r, (
														SELECT COUNT(DISTINCT annotation.resourceid) AS reviewcount, COUNT(*) AS commentcount FROM annotation WHERE personid=?
													) AS c WHERE p.id=?', array($personId, $personId, $personId, $personId));
	$person = $personReq->fetchAssocRow();

	$showReq = DBExecute("SELECT EMAIL, AFFILIATION, DESCRIPTION, SHOWNAME, SHOWAFFILIATION, SHOWJOINDATE, SHOWDESCRIPTION, SHOWFRIENDLIST, SHOWPAPER, SHOWEMAIL, SHOWPUBLICATION FROM DB2INST1.person WHERE ID = ?",$personId);
	
	$showornot = $showReq->fetchAssocRow();
	$hot = $person['LOGINTIMES'];
	$_SESSION["viewperson"] = $person["USERNAME"];//used for adding friends
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="third_party/jquery-ui-1.8.5/jquery-ui-1.8.5.custom.css" />
<link rel="stylesheet" type="text/css" href="css/template.css" />
<link rel="stylesheet" type="text/css" href="css/person_view.css" />
<link rel="stylesheet" type="text/css" href="css/network_styles.css" />
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="third_party/jit.js"></script>
<script type="text/javascript" src="third_party/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="third_party/jquery-ui-1.8.5/jquery-ui-1.8.5.custom.min.js"></script>
<script type="text/javascript" src="js/system.js"></script>
<script type="text/javascript" src="js/timechart.js"></script>
<script type="text/javascript" src="js/network_custom.js"></script>
<script type="text/javascript" src="js/network.js"></script>

<script language="javascript" src="js/bluff/js-class.js" type="text/javascript"></script>
<script language="javascript" src="js/bluff/bluff-min.js" type="text/javascript"></script>
<!--[if IE]><script language="javascript" src="js/bluff/excanvas.js" type="text/javascript"></script><![endif]-->
<script language="javascript" src="js/drawpiechart.js" type="text/javascript"></script>

<script type='text/javascript'>
var personId = <?php echo $personId; ?>;
function GInitialize() {
	var timechart = new TimeChart(document.getElementById("timeline-section"));
	RequestData("data.php", { "requestType": "timechartp", "id": personId }, timechart.update);
}

google.load('visualization', '1', {'packages':['annotatedtimeline']});
google.setOnLoadCallback(GInitialize);

var piechartData = {
		items: [{label: 'Hot', data: <?php echo $hot; ?>},
				{label: 'Publications', data: <?php echo $person['AUTHORCOUNT']; ?>},
		    	{label: 'Comments', data: <?php echo $person['COMMENTCOUNT']; ?>},
		    	{label: 'Reading', data: <?php echo $person['READINGCOUNT']; ?>}]
		    	};

function OnLoad() {
	networkWidget = new Network("network", null, new NetworkCustom('person'));
	RequestData("data.php", { "requestType": "personnetwork", "id": personId }, networkWidget.update);
	drawpiechart(piechartData);
}

//must declare tabbed sections here
$(function() {
	$("#tabs").tabs();
	$("#maintabs").tabs();
	$("input.date").datepicker();

});
</script>
<title>
<?php echo (htmlentities($person["USERNAME"]).'\'s Profile - CoRAL Reporter')?>
</title>
</head>
<body onload="OnLoad();">

	<?php //to determine whether a user has logged in or not, which will output different buttons
	if(isset($_SESSION["accountNo"])){
		$inorout = "controller/logout.php";
		$display = "Log out";
	}
	else{
		$inorout = "login.php";
		$display = "Log in";
	}
	?>

	<div id="body-wrapper">
	<?php include_once 'static/header.php'; ?>
	
		<div id="body">
			<!-- START SECTION: summary -->
			<div id="summary-section" class="section"> <!-- The stats section in the top right hand section of the page -->
				<table id="stat" class="info">
					<tr><td class="labelright"><strong>Papers Published: </strong></td><td><?php echo $person['AUTHORCOUNT']; ?></td>
					<tr><td class="labelright"><strong>Papers Read: </strong></td><td><?php echo $person['READINGCOUNT']; ?></td>
					<tr><td class="labelright"><strong>Papers Reviewed: </strong></td><td><?php echo $person['REVIEWCOUNT']; ?></td>
					<tr><td class="labelright"><strong>Comments: </strong></td><td><?php echo $person['COMMENTCOUNT']; ?></td>
					<tr><td class="labelright"><strong>Latest Activity: </strong></td><td><?php echo $person['LATESTACTIVITY']; ?></td>
				</table>
				
				<table id="summary" class="info"> <!-- The summary section in the top left hand section of the page -->
					<tr>
						<th rowspan="6"><img src="image/person.png" /></th>
						<td colspan="2"><h1><?php echo htmlentities($person['USERNAME']); ?></h1></td>
					</tr>
					<tr><td class="labelright"><strong>Name: </strong></td><td><?php if($showornot["SHOWNAME"] == 'y'||$_SESSION["accountNo"] == $person['USERNAME'])	{	echo htmlentities($person['NAME']);	} ?></td></tr>
					<tr><td class="labelright"><strong>Email: </strong></td><td><?php if($showornot["SHOWEMAIL"] == 'y'||$_SESSION["accountNo"] == $person['USERNAME'])	{	echo htmlentities($person['EMAIL']);	} ?></td></tr>
					<tr><td class="labelright"><strong>Affiliation: </strong></td><td><?php if($showornot["SHOWAFFILIATION"] == 'y'||$_SESSION["accountNo"] == $person['USERNAME'])	{	echo htmlentities($person['AFFILIATION']);	} ?></td></tr>
					<tr><td class="labelright"><strong>Join Date: </strong></td><td><?php if($showornot["SHOWJOINDATE"] == 'y'||$_SESSION["accountNo"] == $person['USERNAME'])	{	echo $person['JOINDATE'];	} ?></td></tr>
					<tr><td class="labelright"><strong>Description: </strong></td><td><?php if($showornot["SHOWDESCRIPTION"] == 'y'||$_SESSION["accountNo"] == $person['USERNAME'])	{	echo htmlentities($person['DESCRIPTION']);	} ?></td></tr>
				</table>
			</div>
			
			<?php
				$isAdminreq = DBExecute("SELECT p.username as name FROM person p where p.id = ?", array($_SESSION["personid"]));
				$adminStatus = $isAdminreq->fetchAssocRow();
			?>			
			<div id="links-section" class="section"> <!-- space below the summery section that displays the links (delete user, follow me, report for abuse etc -->
				<table id="links-table" class="info">
					<tr>
					 <td>
					 
					<?php
					if($_SESSION["accountNo"] != $person["USERNAME"] && $adminStatus["NAME"] == 'administrator'){ 
					?> 
						<a href=controller/deleteUser.php>Delete this user account</a>&nbsp;&nbsp;&nbsp;&nbsp; 
					<?php
					}
					?>
		
					 <?php 
					 if($_SESSION["accountNo"] != $person["USERNAME"] && isset($_SESSION["accountNo"])){
					 	$url = $APIhost.'publishService/friends/listFriends';
						$row = DBExecute("SELECT username, password FROM person WHERE id=?",$_SESSION["personid"])->fetchAssocRow();
						$username = $row['USERNAME'];
						$password = $row["PASSWORD"];
						
						$friendRequestData = array("jaxbBean"=>array(array("key"=>"username", "val"=>$username), array("key"=>"password", "val"=>$password)));
						$post_str_friend = json_encode($friendRequestData);
						$friends = sendToHost($url, "POST", $post_str_friend, 'application/json', 'application/json');
						$_SESSION["myFriendList"] = json_decode($friends);
						
						$flag = false;
						
						if(count($_SESSION["myFriendList"]->user) == 1){ 
							$flag = ($_SESSION["myFriendList"]->user->username == $person["USERNAME"])?true:false;
						}
						else {
							for($i = 0; $i < count($_SESSION["myFriendList"]->user) && $flag == false; $i++){
								$flag = ($_SESSION["myFriendList"]->user[$i]->username == $person["USERNAME"])?true:false;
							}
						}
						
						if($flag == false){ // only display follow me link if they are not already followed
					 		?>
					 		<a href="controller/addfriends.php">Follow me</a>&nbsp;&nbsp;&nbsp;&nbsp;
					 		<?php
					 	}
					 }
					 ?>
					 </td>
					 <?php
					 if($_SESSION["accountNo"] != $person["USERNAME"]){ //only display link if it is not the users own profile
					 ?>
						 <td><a href="report_user.php">Report this user for abuse</a></td>
				     <?php
					 }
					 ?>
					</tr>
				</table>
			</div>
			<!-- END SECTION: summary -->
			
			<div id="details-section" class="section">
				<div id="maintabs" style="overflow:hidden;">
					<ul> 
						<li><a href="#maintabs-1"><strong>Overview</strong></a></li>
						<li><a href="#maintabs-2"><strong>Friends</strong></a></li>
						<li><a href="#maintabs-3"><strong>Publications</strong></a></li>
						<?php
						if($_SESSION["accountNo"] == $person['USERNAME']){	//only display settings tab if the user is logged in, and viewing their own profile
						?>
							<li><a href="#maintabs-4"><strong>Settings</strong></a></li>
						<?php 
						}
						?>
						<!--<li><a href="#maintabs-5">Links to Me</a></li>
						--><li><a href="#maintabs-6"><strong>Watch List</strong></a></li><!--
						<li><a href="#maintabs-7">Friends' News Feed</a></li>
					--></ul>
					
					<div id="maintabs-1"> <!-- Overview tab-->
						<!-- START SECTION: timeline -->
						<fieldset>
						<legend>Time Line
						
						<a href="help.php#timeline" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a>
						</legend>
						<div id="timeline-section" class="section">
				
							<div id="timeline-chart"></div>
						</div>
						</fieldset>
						<!-- END SECTION: summary --> 
						
						<!-- START SECTION: social graph -->
						<div class="left-section">
						
							<fieldset>
							<legend>Reviewing Interactions
							<a href="help.php#network" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a>
							</legend>
								<div id="network-section" >
									<div id="network"></div>
								</div>
							</fieldset>	
								
							<fieldset>
							<legend>Community Score</legend>	
								<div float="left" >
									<canvas id="bluffExample" ></canvas>
								</div>
							</fieldset>					
							
						</div>
						<!-- END SECTION: social graph -->
						
						<!-- START SECTION: recent -->
						<div id="activity-section" class="right-section" style="margin-top: 12px">
							<div id="tabs" style="overflow:hidden;">
								<ul> 
									<li><a href="#tabs-1"><strong>Reviewed Papers</strong></a></li>
									<li><a href="#tabs-2"><strong>Interactions</strong></a></li>
									<li><a href="#tabs-3"><strong>Comments</strong></a></li>
									<li><a href="#tabs-4"><strong>Tags</strong></a></li>
								</ul>
								<div id="tabs-1">  <!-- Reviewed papers tab-->
									<ul class="list">
									<div style="float:right;"><a href="help.php#review" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
									<?php
									$stmt = DBExecute("	SELECT a.*,annotation.datecreated,annotation.annofabid,bibtex.title,bibtex.abstract 
																			FROM (
																				SELECT DISTINCT r.bibtexid,
																					MAX(annotation.id) AS aid,
																					MAX(reading.date) AS rdate,
																					AVG(rateresource.total) AS total 
																				FROM resource AS r,reading,resource AS rr 
																				LEFT JOIN rateresource ON rateresource.resourceid=rr.id 
																				LEFT JOIN annotation ON annotation.resourceid=rr.id 
																				WHERE reading.resourceid=r.id 
																				AND rr.bibtexid = r.bibtexid 
																				AND reading.personid=? 
																				GROUP BY r.bibtexid
																			) AS a 
																			LEFT JOIN bibtex ON bibtex.id = a.bibtexid 
																			LEFT JOIN annotation ON annotation.id = a.aid 
																			ORDER BY a.rdate DESC;", $personId);
									
									while($row = $stmt->fetchAssocRow()) { 
										?>
										<li> <!-- List of the users reviewed papers -->
											
											<!--<div class="rating"><!?php echo empty($row['TOTAL']) ? "N/A" : $row['TOTAL']; ?></div><br>
											-->
											<a href="paper_view.php?id=<?php echo $row['BIBTEXID']; ?>"><span class="title"><?php echo (empty($row['TITLE'])) ? "Paper ".$row['BIBTEXID'] : htmlentities($row['TITLE']); ?></span></a>
											<div class="abstract">
											<?php //echo empty($row['DATECREATED']) ? 'No abstract available.' : 'Abstract: '.substr($row['ABSTRACT'], 200).'...'; ?>
											<small>Visited: <?php echo $row['RDATE'], empty($row['DATECREATED']) ? "" : " / Last update: ", $row['DATECREATED'], empty($row['DATECREATED']) ? "" : " by ", htmlentities(preg_replace('/-.*$/','',$row['ANNOFABID'])); ?></small>
											</div>
										</li>
									<?php
									} ?>
									</ul>
								</div> 
								
								<div id="tabs-2">  <!-- Interactions-->
									<ul class="list">
									<div style="float:right;"><a href="help.php#interaction" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
									<?php
									$row = DBExecute("SELECT username FROM person WHERE id=?",$personId)->fetchAssocRow();
									$username = $row['USERNAME'];
									$personname = $row['USERNAME']."-%";
									$stmt = DBExecute("	SELECT c.*,person.email,person.name,person.id,person.affiliation
					                            FROM (
					                              SELECT b.count,b.other,annotation.datecreated,annotation.resourceid,bibtex.title
					                              FROM (
					                                SELECT COUNT(*) AS count, a.other, MAX(a.id) AS aid
					                                FROM (
					                                  SELECT SUBSTR(replytofabid,1,LOCATE('-',replytofabid)-1) AS other,
					                                  datecreated,resourceid,id 
					                                  FROM annotation 
					                                  WHERE annofabid LIKE ? 
					                                  AND replytofabid IS NOT NULL 
					                                  AND replytofabid NOT LIKE ?
					                                  UNION SELECT SUBSTR(annofabid,1,LOCATE('-',annofabid)-1) AS other,
					                                  datecreated,resourceid,id
					                                  FROM annotation 
					                                  WHERE replytofabid LIKE ? 
					                                  AND annofabid NOT LIKE ?
					                                ) AS a
					                                GROUP BY a.other
					                              ) AS b,annotation,resource
					                              JOIN bibtex ON bibtex.id = resource.bibtexid
					                              WHERE annotation.id = b.aid 
					                              AND resource.id = annotation.resourceid
					                            ) AS c,person
					                            WHERE person.username = c.other ORDER BY datecreated DESC", array($personname, $personname, $personname, $personname));
					
									///TODO this may cause errors. instead of 'other' we should find the personid and then username
									while ($row = $stmt->fetchAssocRow()) {
										if ($row['OTHER']) { ?>
											<li><a href="person_view.php?id=<?php echo $row['ID']; ?>">
													<img src="image/person.png" height="80px"/><?php echo htmlentities(preg_replace('/-.*$/','',$row['OTHER'])); ?>:
												<span class="interactions"><span class="unbold"><?php echo $row['COUNT']; ?> interactions</span></span><br />
												<?php $info=getInfo($row['ID']);?>
												Name: <span class="unbold"><?php echo htmlentities($info['NAME']); ?></span><br />
												E-mail: <span class="unbold"><?php echo htmlentities($info['EMAIL']); ?></span><br />
												Affiliation: <span class="unbold"><?php echo htmlentities($info['AFFILIATION']); ?></span><br />
												<small>Last comment on <?php echo $row['DATECREATED']; ?> on <?php echo (empty($row['TITLE'])) ? "Paper ".$row['RESOURCEID'] : htmlentities($row['TITLE']); ?></small>
											</a></li>
										<?php
										}
									} ?>
									</ul>
								</div>
								
								<div id="tabs-3">  <!-- Comments -->
									<ul class="list">
									<div style="float:right;"><a href="help.php#comments" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
									<?php
									$stmt = DBExecute("	SELECT a.*, bibtex.title, bibtex.id AS bid
																			FROM (
																				SELECT a.ratebyanno, a.resourceid, a.annotationbody, a.datecreated AS date, r.bibtexid, a.type 
																				FROM annotation AS a JOIN resource AS r ON r.id = a.resourceid
																				WHERE a.personid = ?
																			) AS a
																			JOIN bibtex ON bibtex.id=a.bibtexid
																			ORDER BY date DESC", $personId);
					
									while ($row = $stmt->fetchAssocRow()) {  
										if($row['TYPE'] != 'highlight'){
											
										?> <!-- list of comments by the user -->
										<li><a href="paper_view.php?id=<?php echo $row['BID']; ?>">
											<span class="comment_body"><?php
											$annobody = preg_replace('/(.*[\n|\r]*.*<content>[\n|\r]*|\s*<\/content>[\n|\r]*.*)/','',$row['ANNOTATIONBODY']);
											echo htmlentities($annobody); 
											?></span><br />
											<span class="unbold">: Specified attitude
											<img float="right" src="image/<?php
											switch($row['RATEBYANNO']) {
											case 1: echo "Pos"; break;
											case 2: echo "Neg"; break;
											case 4: echo "Info"; break;
											default: echo "Neut"; break;
											} ?>.gif">
											</img></span><br />
											<small>Made on <?php echo empty($row['DATE']) ? "None" : $row['DATE'] ?> on <?php echo (empty($row['TITLE'])) ? "Paper ".$row['BID'] : htmlentities($row['TITLE']); ?></small>
										</a></li>
									<?php
										}
									} ?>
									</ul>
								</div>
								<div id="tabs-4"> <!-- Tags -->
									<div id="filter-section">
									<fieldset>
									<legend>Read Paper's Tags</legend>									
									<div id="filter">										
										<div id="tags-simple" class="tags">										
											<ul> 
												<?php												
												$tags = getTagsOfReadPapers($personId);
												foreach ($tags as $tag => $freq) {
													$tag = htmlentities($tag);
													echo '<li class="tag', $freq, '"><a href="tag_papers.php?label=',$tag,'">', $tag, '</a></li>';
												} ?>												
											</ul>
										</div>
									</div>
									</fieldset>
									<br/>
									<br/>
									<fieldset>
									<legend>Tags Assigened by User</legend>
									<div id="filter">										
										<div id="tags-simple" class="tags">										
											<ul> 
												<?php
												$tags = getTagsAssignedByUser($personId);
												foreach ($tags as $tag => $freq) {
													$tag = htmlentities($tag);
													echo '<li class="tag', $freq, '"><a href="tag_papers.php?label=',$tag,'">', $tag, '</a></li>';
												} ?>												
											</ul>
										</div>
									</div>
									</fieldset>
									</div>
								</div>
							</div>	
						</div>
						
						<!-- END SECTION: recent --> 								
					</div> 
					
					<div id="maintabs-2"> <!-- Friends tab-->
					<div style="float:right;"><a href="help.php#friends" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
					<?php
					if($showornot["SHOWFRIENDLIST"] == 'y'||$_SESSION["accountNo"] == $person['USERNAME'])
					{
						//show all friendship requests, don't need any more
						?>
						<div>
							<ul class="list">
								<?php 
									$url = 'http://hypatia.cs.ualberta.ca:9999/publishService/friends/listFriends';
									$row = DBExecute("SELECT username, password FROM person WHERE id=?",$personId)->fetchAssocRow();
									$username = $row['USERNAME'];
									$password = $row["PASSWORD"];
									
									$friendRequestData = array("jaxbBean"=>array(array("key"=>"username", "val"=>$username), array("key"=>"password", "val"=>$password)));
									$post_str_friend = json_encode($friendRequestData);
									$friends = sendToHost($url, "POST", $post_str_friend, 'application/json', 'application/json');
									$friendList = json_decode($friends);
									
									for($i = 0; $i < count($friendList->user); $i++){
										if(count($friendList->user) == 1){
											$row = DBExecute("SELECT * FROM person WHERE username=?",$friendList->user->username);
										}
										else {
											$row = DBExecute("SELECT * FROM person WHERE username=?",$friendList->user[$i]->username);
										}
										if($rowr = $row->fetchAssocRow()){
										?>
										<li><a href="person_view.php?id=<?php echo $rowr['ID']; ?>">
													<img src="image/person.png" height="60px"/>
													
												<?php echo htmlentities($rowr['USERNAME'])?><br/>
												<?php $info=getInfo($rowr['ID']);?>
												Name: <span class="unbold"><?php echo htmlentities($info['NAME']); ?></span><br />
												E-mail: <span class="unbold"><?php echo htmlentities($info['EMAIL']); ?></span><br />
												Affiliation: <span class="unbold"><?php echo htmlentities($info['AFFILIATION']); ?></span><br />
												<small></small>
											</a></li>
											<br />
										<?php
										}
									}
								?>
							</ul>
						</div>
						<?php
					}
					else {
						echo "Friends list is chosen to be private.";
					}
					?>
					</div> 
					<div id="maintabs-3"> <!-- Publications tab-->
					
<!--						<div style="float:right;"></div>-->
						<div id="publications">
						<!--
							<h3><!?php echo $person['NAME']; ?>'s Publications</h3>	
							-->
							
							<!-- Show publications -->
							<div id="current-publications" style="display:block"> <!-- List of the users current publciations -->
							<fieldset>
							<legend><?php echo htmlentities($person['NAME']); ?>'s Publications 
							<a href="help.php#publication" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a>
							</legend>
								<ul class="list">
					   			<?php
					   			
					   			$paperlistReq = DBExecute("SELECT bibtexid, title, authors from authoringbib, bibtex where personid=? and id=bibtexid", $personId);
					   			
					   			if($showornot["SHOWPUBLICATION"] == 'y' || $person['USERNAME'] == $_SESSION["accountNo"]){	
										//show publications
									while($pub = $paperlistReq->fetchAssocRow()) { 
										?>
										<li>
											<a href="paper_view.php?id=<?php echo $pub['BIBTEXID']; ?>"><span class="title"><?php echo (empty($pub['TITLE'])) ? "Paper ".$pub['BIBTEXID'] : htmlentities($pub['TITLE']); ?></span></a>
											<div class="abstract">
											<!--<div class="rating"> <small>Rating: <!?php echo empty($row['TOTAL']) ? "N/A" : $row['TOTAL']; ?></small></div>
											-->
											<?php //echo empty($row['DATECREATED']) ? 'No abstract available.' : 'Abstract: '.substr($row['ABSTRACT'], 200).'...'; ?>
											<?php
											/*$pubinfoReq = DBExecute("SELECT max(datecreated) as datecreated, username from annotation a, person p, resource r 
																		where p.id = a.personid and 
																		a.resourceid = r.id 
																		and r.bibtexid = ?
																		", $pub['BIBTEXID']);*/ 
											?>
											<small>Authors: <?php echo isset($pub['AUTHORS']) ? htmlentities($pub['AUTHORS']) : 'Not provided' ?></small>
											</div>
										</li>
									    
									<?php
									}
								}
								else if($person['USERNAME'] != $_SESSION["accountNo"]){
									echo "Publications list is chosen to be private.";
								}?>
								
								
					   			
					   			<?php 
								/*$stmt = DBExecute("	SELECT a.*,annotation.datecreated,annotation.annofabid,bibtex.title,bibtex.abstract 
													FROM (
													SELECT DISTINCT r.bibtexid,
														MAX(annotation.id) AS aid,
														MAX(reading.date) AS rdate,
														AVG(rateresource.total) AS total 
													FROM resource AS r,reading,resource AS rr 
													LEFT JOIN rateresource ON rateresource.resourceid=rr.id 
													LEFT JOIN annotation ON annotation.resourceid=rr.id
												        LEFT JOIN authoringbib ON authoringbib.bibtexid=rr.bibtexid
												        LEFT JOIN person ON person.id=authoringbib.personid 
													WHERE reading.resourceid=r.id 
														AND rr.bibtexid = r.bibtexid
												        AND authoringbib.personid=?
												        AND rr.bibtexid IS NOT NULL
														GROUP BY r.bibtexid
													) AS a 
													LEFT JOIN bibtex ON bibtex.id = a.bibtexid 
													LEFT JOIN annotation ON annotation.id = a.aid 
													ORDER BY a.rdate DESC;", $personId);
								if($showornot["SHOWPUBLICATION"] == 'y'){	
										//show publications
									while($row = $stmt->fetchAssocRow()) { */
										?>
										
									<?php
									/*}
								}
								else {
									echo "This person doesn't allow others to see his/her publications.";
								}*/?>
								</ul>
								</fieldset>
							</div>


							<?php 
								
							?>							
							
<!--							<a href="#maintabs-3" onclick="javascript:showElement('addPaper'); javascript:showElement('current-publications')">Add a Publication</a>				-->

							
							
							<div id="addPaper" class = "section">
							<?php if($person['USERNAME'] == $_SESSION["accountNo"]){?>
							<fieldset>
							<legend>Add Publication 
							<a href="help.php#publication" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a>
							</legend>
								<!-- // in order not to show add publication to others except the user himself/herself-->
								<?php
								

									/*if(isset($_SESSION["successmsg"])){
										echo $_SESSION["successmsg"]."<br/>";
										$_SESSION["successmsg"]=null;
									}*/
									
									//Get user ID to anchor the form when user clicks on the Claim Paper button
									$selectid = DBExecute("SELECT id FROM person WHERE username = ?",$_SESSION["accountNo"]);
									$id = $selectid->fetchAssocRow();//find the person's id to redirect to the person's own page
									$url = "../person_view.php?id=".$id["ID"]."#maintabs-3"; 									
								?>
									<!-- Form to add a new publication-->
									<form id="claimPaper" action="controller/addPublication.php" method="post" >
										<table class="info">
											<tr><strong><?php echo $_SESSION["successmsg"]; $_SESSION["successmsg"]=null;?></strong></tr>
											<tr><strong><?php echo $_SESSION["errmsg"]; $_SESSION["errmsg"]=null;?></strong></tr>
											<tr><td></td><td class="labelleft"><font size="1">Note that the fields marked with a '*' are required</font></td></tr>
											<tr><td class="labelright"><strong>Title:</strong><font color="red">*</font></td><td><input type="text" name="title" size="40"/></td></tr>
											<tr><td class="labelright"><strong>Author(s):</strong><font color="red">*</font></td><td><input type="text" name="authors" size="40"/></td></tr>
											<tr><td></td><td class="labelleft"><font size="1">Separate multiple authors with semicolons</font></td></tr>
<!--											<tr><td class="labelright"><strong>Abstract:</strong></td><td><input type="text" name="abstract" size="40"/></td></tr>-->
											<tr><td class="labelright"><strong>Abstract:</strong></td><td><textarea name="abstract" rows="5" cols="31"></textarea></td></tr>
											<tr><td class="labelright"><strong>DOI:</strong></td><td><input type="text" name="doi" size="40"/></td></tr>
											<tr><td class="labelright"><strong>ISBN:</strong></td><td><input type="text" name="isbn" size="40"/></td></tr>
											<tr><td class="labelright"><strong>Journal:</strong></td><td><input type="text" name="journal" size="40"/></td></tr>
											<tr><td class="labelright"><strong>Volume:</strong></td><td><input type="text" name="volume" size="40"/></td></tr>
											<tr><td class="labelright"><strong>Issue:</strong></td><td><input type="text" name="issue" size="40"/></td></tr>
											<tr><td class="labelright"><strong>Pages:</strong></td><td><input type="text" name="pages" size="40"/></td></tr>
											<tr><td class="labelright"><strong>Year of Publication:</strong></td><td><input type="text" name="year" size="40"/></td></tr>
											<tr><td class="labelright"><strong>File Location:</strong></td><td><input type="file"  name="file" id="file" size="40" /></td></tr>
											
											<tr><td></td><td><input type="submit" class="button" value="Claim Paper" /></td></tr>
											
										</table>
										<input type="hidden" name="submitCheck" value=true>																				
									</form>
								
								</fieldset>
								<?php		
								}
								/*else {							
									echo 'You cannot add publication for others.';
								}*/
								?>
							</div>



							
						</div>				
					</div> 
					<div id="maintabs-4"> <!-- Settings tab-->
					<?php
						if($_SESSION["accountNo"] == $person['USERNAME'])
						{
					?>
						<div style="float:right;"><a href="help.php#setting" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
					<?php 
						}	
					?>
					<?php //content in settings
						if($person['USERNAME'] == $_SESSION["accountNo"])//only the person himself/herself can view his/her own settings
						{	
							//TO_DO show whether it's public or not
							if($showornot["SHOWNAME"] == 'y'){
								$publicInfo["name"] = 'public';
							}
							else{
								$publicInfo["name"] = 'private';
							}
							
							if($showornot["SHOWEMAIL"] == 'y'){
								$publicInfo["email"] = 'public';
							}
							else{
								$publicInfo["email"] = 'private';
							}
							
							if($showornot["SHOWAFFILIATION"] == 'y'){
								$publicInfo["affiliation"] = 'public';
							}
							else{
								$publicInfo["affiliation"] = 'private';
							}
							
							if($showornot["SHOWJOINDATE"] == 'y'){
								$publicInfo["joindate"] = 'public';
							}
							else{
								$publicInfo["joindate"] = 'private';
							}
							
							if($showornot["SHOWDESCRIPTION"] == 'y'){
								$publicInfo["description"] = 'public';
							}
							else{
								$publicInfo["description"] = 'private';
							}
							
							if($showornot["SHOWFRIENDLIST"] == 'y'){
								$publicInfo["friendlist"] = 'public';
							}
							else{
								$publicInfo["friendlist"] = 'private';
							}
							
							if($showornot["SHOWPAPER"] == 'y'){
								$publicInfo["paperread"] = 'public';
							}
							else{
								$publicInfo["paperread"] = 'private';
							}
							
							if($showornot["SHOWPUBLICATION"] == 'y'){
								$publicInfo["publication"] = 'public';
							}
							else{
								$publicInfo["publication"] = 'private';
							}
							
							?>
							<h3><?php echo htmlentities($person['NAME']); ?>'s Settings</h3> 	
							<form name="name" action="controller/changesetting.php" method="post"> <!-- Form for changing the users settings-->
								<table>
									<tr><strong><?php echo $_SESSION["errmsg"]; ?></strong></tr>
									
									<tr>
										<td class="labelright"><strong>New Password:</strong></td>
										<td><input type="password" name="newpassword" maxlength="20" size="20" value="" /></td>
									</tr>
	
									<tr>
										<td class="labelright"><strong>Confirm Password:</strong></td>
										<td><input type="password" name="confirmpassword" maxlength="20" size="20" value="" /></td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Affiliation:</strong></td>
										<td><input type="text" name="newaffiliation" maxlength="40" size="20" value="<?php echo htmlentities($showornot["AFFILIATION"]); ?>" /></td>
									</tr>
									<tr>
										<td></td>
										<td>
										<input type="radio" name="affiliation" value="public" <?php if($showornot["SHOWAFFILIATION"]=='y')	{	echo "checked";  } ?>/>
										<label for="affiliation">public</label>
										<input type="radio" name="affiliation" value="private" <?php if($showornot["SHOWAFFILIATION"]!='y')	{	echo "checked";  } ?>/>
										<label for="affiliation">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Description:</strong></td>
										<td><input type="text" name="newdescription" maxlength="200" size="20" value="<?php echo htmlentities($showornot["DESCRIPTION"]); ?>" /></td>
									</tr>
									<tr>
										<td></td>
										<td>
										<input type="radio" name="description" value="public" <?php if($showornot["SHOWDESCRIPTION"]=='y')	{	echo "checked";  } ?>/>
										<label for="description">public</label>
										<input type="radio" name="description" value="private" <?php if($showornot["SHOWDESCRIPTION"]!='y')	{	echo "checked";  } ?>/>
										<label for="description">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Email:</strong></td>
										<td><input type="text" name="newemail" maxlength="40" size="20" value="<?php echo htmlentities($showornot["EMAIL"]); ?>" /></td>
										<tr>
										<td></td>
										<td>
										<input type="radio" name="email" value="public" <?php if($showornot["SHOWEMAIL"]=='y')	{	echo "checked";  } ?>/>
										<label for="email">public</label>
										<input type="radio" name="email" value="private" <?php if($showornot["SHOWEMAIL"]!='y')	{	echo "checked";  } ?>/>
										<label for="email">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Name:</strong></td>
										<td>
										<input type="radio" name="radio" value="public" <?php if($showornot["SHOWNAME"]=='y')	{	echo "checked";  } ?>/>
										<label for="name">public</label>
										<input type="radio" name="radio" value="private" <?php if($showornot["SHOWNAME"]!='y')	{	echo "checked"; } ?>/>
										<label for="name">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Member Since:</strong></td>
										<td>
										<input type="radio" name="joindate" value="public" <?php if($showornot["SHOWJOINDATE"]=='y')	{	echo "checked";  } ?>/>
										<label for="joindate">public</label>
										<input type="radio" name="joindate" value="private" <?php if($showornot["SHOWJOINDATE"]!='y')	{	echo "checked";  } ?>/>
										<label for="joindate">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Friends List:</strong></td>
										<td>
										<input type="radio" name="friendlist" value="public" <?php if($showornot["SHOWFRIENDLIST"]=='y')	{	echo "checked";  } ?>/>
										<label for="friendlist">public</label>
										<input type="radio" name="friendlist" value="private" <?php if($showornot["SHOWFRIENDLIST"]!='y')	{	echo "checked";  } ?>/>
										<label for="friendlist">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Papers Read:</strong></td>
										<td>
										<input type="radio" name="paperread" value="public" <?php if($showornot["SHOWPAPER"]=='y')	{	echo "checked";  } ?>/>
										<label for="paperread">public </label>
										<input type="radio" name="paperread" value="private" <?php if($showornot["SHOWPAPER"]!='y')	{	echo "checked";  } ?>/>
										<label for="paperread">private</label>
										</td>
									</tr>
									
									<tr>
										<td class="labelright" ><strong>Publication:</strong></td>
										<td>
										<input type="radio" name="publication" value="public" <?php if($showornot["SHOWPUBLICATION"]=='y')	{	echo "checked";  } ?>/>
										<label for="publication">public </label>
										<input type="radio" name="publication" value="private" <?php if($showornot["SHOWPUBLICATION"]!='y')	{	echo "checked";  } ?>/>
										<label for="publication">private</label>
										</td>
									</tr>
									
									<tr>
										<td></td>
										<td>
										<input type="submit" class="button" value="submit" />
										</td>
									</tr>
								</table>
							</form>
							<?php
						}
						
						else {
							//show nothing
						}
					?>
					</div> 
					<!--<div id="maintabs-5">  Links to Me tab
						<div style="float:right;"><a href="help.php#link" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
						<div>
							
						</div>
					</div>
					
					--><div id="maintabs-6"> <!-- Watch List tab-->
						<div style="float:right;"><a href="help.php#watchlist" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
							<ul class="list">
								<?php
								$stmt = DBExecute("	SELECT bibtex.id as BIBTEXID, bibtex.title as TITLE FROM watchlist, bibtex 
														WHERE watchlist.resourceid = bibtex.id 
														  and watchlist.personid = ?", $personId);
								while($row = $stmt->fetchAssocRow()) { 
									?>
									<li>
										<a href="paper_view.php?id=<?php echo $row['BIBTEXID']; ?>"><span class="title"><?php echo (empty($row['TITLE'])) ? "Paper ".$row['BIBTEXID'] : htmlentities($row['TITLE']); ?></span></a>
										
										<!-- <div class="abstract">
											<small>Added on: <!?php echo $row['DATECREATED']; ?></small>
										</div>  -->
										
									</li>
								<?php
								} ?>
							</ul>
					</div>
					
					<!--<div id="maintabs-7">  Friends Watchlist tab
						<div style="float:right;"><a href="help.php#news" onClick="return popup(this, 'notes')"><img alt="help" src="image/question.png" /></a></div>
						<div>
						</div>
					</div>
				--></div> 
			</div>
		</div>
	<?php include_once 'static/footer.html'; ?>
	</div>
</body>
</html>
